package de.hmm.hwip.osgi.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ScrollView;
import android.widget.TextView;
import de.hmm.hwip.osgi.R;
import de.hmm.hwip.service.FelixStarter;

/**
 * Example of a Gui which is connected to the osgi Framework. 
 * It receives data send by OSGi Messagehandler 
 * @author johannes mendel
 *
 */
public class OSGiAdminConsole extends Activity {

	/** Class identifier **/
	final private String TAG = "OSGiAdminConsole-GUI"; 
	TextView bundleText;
	TextView stateText;
	static TextView messageData;
	ScrollView rScrollView;
	static Activity appActivity;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        appActivity = this;
        
        setContentView(R.layout.activity_osgi_admin_console);
        startService(new Intent(this, FelixStarter.class));
        
        bundleText = (TextView) findViewById(R.id.bundleTextView);
        stateText = (TextView) findViewById(R.id.bundleStateTextView);
        rScrollView = (ScrollView) findViewById(R.id.scrollViewR);
        messageData = (TextView) findViewById(R.id.messageDataTextView);
    	messageData.setMaxLines(100);
        
        bundleText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				listBundleStates();
			}
		});
        
        rScrollView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				listBundleStates();
			}
		});
        
        listBundleStates();
    }
    
    /** Method to list all bundles and there osgi-state**/
    private void listBundleStates(){
    	String[] bundles = FelixStarter.getInstalledBundleState();
		bundleText.setText(bundles[0]);
		stateText.setText(bundles[1]);		
    }
    
    /** writes data in list **/ 
    public static synchronized void listDataValues(final String newData){
    	 if (appActivity != null) {
    		 /*activity.runOnUiThread(
    		  * Runs the specified action on the UI thread. If the current thread is the 
    		  * UI thread, then the action is executed immediately. If the current thread 
    		  * is not the UI thread, the action is posted to the event queue of the UI thread.
    		  * */
    		 appActivity.runOnUiThread(new Runnable() {
    			 @Override
    			 public void run() {
    				 String tempData = newData;
    				 String oldData = messageData.getText().toString();
    				 tempData += "\n " +oldData;
    				 messageData.setText(tempData);
    			 }
    		 });
    	 }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_osgi_admin_console, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_restart:
                return restartOSGi();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    /** restarts the osgi framework **/
    private boolean restartOSGi(){
    	Intent OSGiIntent = new Intent(this, FelixStarter.class);
    	stopService(OSGiIntent);
        synchronized (this) {
        	try {
				wait(2000);
			} catch (InterruptedException e) {
				Log.e(TAG, "Wait exception: " + e.getMessage());
			}
			startService(OSGiIntent);
		}
		return true;
    }
}
